package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.WebVisitDao;
import com.itheima.pojo.WebVisit;
import com.itheima.service.WebVisitService;
import com.itheima.utils.DateUtils;
import com.itheima.vo.WebVisitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class WebVisitServiceImpl implements WebVisitService {
    @Autowired
    private WebVisitDao webVisitDao;

    //获取最近一周用户独立IP数和访问量
    @Override
    public Map<String, Object> getVisitByWeek() {
        // 获取到今天结束的时间
        String todayEndTime = DateUtils.getToDayEndTime();
        //获取最近七天的日期
        Date sevenDaysDate = DateUtils.getDate(todayEndTime, -6);
        String sevenDays = DateUtils.getOneDayStartTime(sevenDaysDate);
        // 获取最近七天的数组列表
        List<String> sevenDaysList = DateUtils.getDaysByN(7, "yyyy-MM-dd");
        // 获得最近七天的访问量
        List<Map<String, Object>> pvMap = webVisitDao.getPVByWeek(sevenDays, todayEndTime);
        // 获得最近七天的独立用户
        List<Map<String, Object>> uvMap = webVisitDao.getUVByWeek(sevenDays, todayEndTime);
        Map<String, Object> countPVMap = new HashMap<>();
        Map<String, Object> countUVMap = new HashMap<>();

        for (Map<String, Object> item : pvMap) {
            countPVMap.put(item.get("DATE").toString(), item.get("COUNT"));
        }
        for (Map<String, Object> item : uvMap) {
            countUVMap.put(item.get("DATE").toString(), item.get("COUNT"));
        }
        // 访问量数组
        List<Integer> pvList = new ArrayList<>();
        // 独立用户数组
        List<Integer> uvList = new ArrayList<>();

        for (String day : sevenDaysList) {
            if (countPVMap.get(day) != null) {
                Number pvNumber = (Number) countPVMap.get(day);
                Number uvNumber = (Number) countUVMap.get(day);
                pvList.add(pvNumber.intValue());
                uvList.add(uvNumber.intValue());
            } else {
                pvList.add(0);
                uvList.add(0);
            }
        }

        Map<String, Object> resultMap = new HashMap<>();

        // 不含年份的数组格式
        List<String> resultSevenDaysList = DateUtils.getDaysByN(7, "MM-dd");

        resultMap.put("date", resultSevenDaysList);
        resultMap.put("pv", pvList);
        resultMap.put("uv", uvList);

        return resultMap;
    }

    @Override
    public Integer getWebVisitCount() {
        WebVisit webVisit = new WebVisit();
        webVisit.setStatus(1);
        return webVisitDao.selectCount(webVisit);
    }

    @Override
    public Page<WebVisit> getList(WebVisitVO webVisitVO) {
        //当前页
        Integer currentPage = webVisitVO.getCurrentPage();
        //每页数据
        Integer pageSize = webVisitVO.getPageSize();
        //分页查询
        PageHelper.startPage(currentPage, pageSize);

        //查询条件构造
        Example example=new Example(WebVisit.class);
        Example.Criteria criteria = example.createCriteria();
        //状态为1的才可以被查询到
        criteria.andEqualTo("status", 1);

        return (Page<WebVisit>) webVisitDao.selectByExample(example);
    }
}
